package belastungstest.mobapp.fhfl.mobappbelastungstest;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.CombinedXYChart;
import org.achartengine.chart.CombinedXYChart.XYCombinedChartDef;
import org.achartengine.chart.LineChart;
import org.achartengine.chart.PointStyle;
import org.achartengine.chart.ScatterChart;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import java.sql.Time;

/**
 * Created by illu on 02.07.2014.
 */
public class Chart
{
    private GraphicalView graphicalView = null;
    private XYSeries dbytes = new XYSeries("kbyte/sec",0);
    private XYSeries dlatency = new XYSeries("latency/sec",1);
    private XYSeries dobytes = new XYSeries("obytes",2);

    private XYSeries avgLatency = new XYSeries("avgLatency/sec",1);

    private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();

    private XYSeriesRenderer renderer = new XYSeriesRenderer();
    private XYSeriesRenderer renderer2 = new XYSeriesRenderer();
    private XYSeriesRenderer renderer3 = new XYSeriesRenderer();
    private XYSeriesRenderer renderer4 = new XYSeriesRenderer();
    private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer(4);

    private XYCombinedChartDef[] types;
    private CombinedXYChart combinedXYChart;


    public  Chart()
    {
        mDataset.addSeries(0,dbytes);
        mDataset.addSeries(1,dlatency);
        mDataset.addSeries(2,dobytes);
        mDataset.addSeries(3,avgLatency);

        //graph render:
       // renderer.setDisplayBoundingPoints(true);
       // renderer.setPointStrokeWidth(1);
        renderer.setLineWidth(3);
        renderer.setDisplayBoundingPoints(true);
        renderer.setColor(Color.YELLOW);
        //renderer.setPointStyle(PointStyle.CIRCLE);
        //renderer.setFillPoints(true);
        renderer.setChartValuesSpacing(10f);

        //graph render2:
        renderer2.setPointStrokeWidth(3);
        renderer2.setDisplayBoundingPoints(true);
        renderer2.setColor(Color.rgb(000, 255, 0));
        //renderer2.setPointStyle(PointStyle.CIRCLE);
        //renderer2.setFillPoints(true);

        //graph render3:
        renderer3.setPointStrokeWidth(1);
        renderer3.setDisplayBoundingPoints(true);
        renderer3.setColor(Color.RED);
        //renderer3.setPointStyle(PointStyle.CIRCLE);
        //renderer3.setFillPoints(true);

        renderer4.setColor(Color.GRAY);
        renderer4.setLineWidth(2);

        //build graph1+2
        mRenderer.addSeriesRenderer(renderer);
        mRenderer.addSeriesRenderer(renderer2);
        mRenderer.addSeriesRenderer(renderer3);
        mRenderer.addSeriesRenderer(renderer4);


        //diagramm render:

        //color
        mRenderer.setApplyBackgroundColor(true);
        mRenderer.setBackgroundColor(Color.BLACK);
        mRenderer.setMarginsColor(Color.BLACK);
        mRenderer.setLabelsColor(Color.WHITE);

        //axes
        mRenderer.setYAxisMin(0,0);
        mRenderer.setYAxisMin(0,1);
        mRenderer.setYAxisMin(0,2);
        mRenderer.setYAxisMin(0,3);
        mRenderer.setAxesColor(Color.BLACK);
        mRenderer.setAxisTitleTextSize(15f);
        mRenderer.setYAxisAlign(Paint.Align.LEFT,0);
        mRenderer.setYAxisAlign(Paint.Align.RIGHT, 1);
        mRenderer.setYAxisAlign(Paint.Align.LEFT,2);

        //label
        mRenderer.setLabelsColor(Color.WHITE);
        mRenderer.setLabelsTextSize(15f);

        //grid:
       // mRenderer.setShowGrid(true);
        //mRenderer.setGridColor(Color.GRAY);

        //text:
        mRenderer.setChartTitle("");
        mRenderer.setYTitle("data in Kbytes",0);
        mRenderer.setYTitle("latency in ms",1);
        mRenderer.setXTitle("time in s");

        //zoom:
        mRenderer.setZoomEnabled(true, true);
        mRenderer.setZoomButtonsVisible(true);
        mRenderer.setAntialiasing(false);

        //merge in one def.
        types =new XYCombinedChartDef[]
        {
                new XYCombinedChartDef(LineChart.TYPE,0),
                new XYCombinedChartDef(ScatterChart.TYPE,1),
                new XYCombinedChartDef(LineChart.TYPE,2),
                new XYCombinedChartDef(LineChart.TYPE,3)
        };

    }

    public GraphicalView getView( Context context)
    {
        graphicalView = ChartFactory.getCombinedXYChartView(context, mDataset, mRenderer, types);
        //graphicalView = ChartFactory.getCombinedXYChartView(context, mDataset, mRenderer, types);
        return graphicalView;
    }


    public void redraw()
    {
        if( graphicalView == null )
            return;

        double maxX = dlatency.getMaxX();
        double minX = maxX - 20000; // deltaX is your required x-range

        mRenderer.setXAxisMax(maxX);
        mRenderer.setXAxisMin(minX);

        graphicalView.repaint();
    }

    public void addNewLatencyPoint(long x, int y)
    {
        dlatency.add( x, y );
    }

    public void addNewBandwithPoint(long x, double y)
    {
        dbytes.add( x, y );
    }

    public void setAvgLatency(double y)
    {
        for( int i=0, e=avgLatency.getItemCount(); i < e; i++ )
            avgLatency.remove(0);

        avgLatency.add( dlatency.getMaxX(), y );
        avgLatency.add( dlatency.getMinX(), y );
    }

}
